package com.eazyimage.lab.service;

import com.eazyimage.lab.entity.Dataset_;
import com.eazyimage.lab.mapper.AddDatasetMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eazyimage.lab.service.Verifygenservice;

import java.io.File;

@Service
public class AddDatasetService {
    @Autowired
    AddDatasetMapper am;
    @Autowired
    Verifygenservice vg;
    @Autowired
    DeleteDatasetService service;
    Dataset_ data_ = new Dataset_();
    public boolean AddDataset(String datasetName, String datasetDescription,
                       String username, int count,
                       int photo_width, int photo_height,
                       int ispublic, String label_list
                       )
    {
        try
        {
            data_.setDatasetName(datasetName);
            data_.setDatasetDescription(datasetDescription);
            data_.setCount(count);
            data_.setPhoto_width(photo_width);
            data_.setPhoto_height(photo_height);
            data_.setIs_public(ispublic);
            data_.setLabel_list(label_list);
            data_.setLabel_default(label_list.split(",")[0]);
            data_.setUsername(username);
            am.AddDataset(data_);
        }catch(Exception ex){
            ex.printStackTrace();
            return false;
        }

        int datasetid = data_.getDatasetid();
        System.out.println(datasetid+"\n");
        vg.Generateverify(datasetid);

        try{
            //String path = "F://have";
            String path = "/easyimage/backend-Flask/static/dataset/" + datasetid;
            File newdataset = new File(path);

            if(!newdataset.exists()){
                if(!newdataset.mkdir()){
                    service.DeleteDataset(datasetid);
                    return false;
                }
                //System.out.println(newdataset.getAbsolutePath());
                //System.out.println(newdataset.mkdir());
            }
        }catch (Exception e){
            service.DeleteDataset(datasetid);
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
